package ar.com.unlp.fi.lrs.persist;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import ar.com.unlp.fi.lrs.service.StatementService;

public class HibernateUtil {

	
	private static final SessionFactory sessionFactory = buildSessionFactory();
	
    private final static Logger logger = Logger.getLogger(StatementService.class);

	
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();
 
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
         
            logger.error(ex.getMessage(), ex);
            
            throw new ExceptionInInitializerError(ex);
        }
    }
 
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
 
    public static void shutdown() {
    	// Close caches and connection pools
    	getSessionFactory().close();
    }
	
	
}
